home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / isapi / ISAPIRequest.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  12.1 KB  |  482 lines

  1. /*
  2.  * @(#)ISAPIRequest.java    1.13 97/06/16
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.isapi;
  23.  
  24. import java.io.IOException;
  25. import java.util.Date;
  26. import java.util.Dictionary;
  27. import java.util.Enumeration;
  28. import java.util.Hashtable;
  29. import javax.servlet.ServletInputStream;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpUtils;
  32. import java.util.StringTokenizer;
  33.  
  34. /**
  35.  * This class represents a servlet request in the ISAPI servlet runner.
  36.  *
  37.  * @version    1.13, 06/16/97
  38.  * @author    David Connelly
  39.  * @author    Jongyoon Lee
  40.  */
  41. class ISAPIRequest implements HttpServletRequest {
  42.     /*
  43.      * The ISAPI connection for this request.
  44.      */
  45.     private ISAPIConnection conn;
  46.  
  47.     /*
  48.      * The input stream for this request.
  49.      */
  50.     private ISAPIInputStream in = new ISAPIInputStream();
  51.  
  52.     /*
  53.      * The parameters for this request.
  54.      */
  55.     private Hashtable params;
  56.  
  57.     /*
  58.      * The headers for this request.
  59.      */
  60.     private Hashtable headers;
  61.  
  62.     /*
  63.      * The REQUEST_URI for this request.
  64.      */
  65.     private String requestURI;
  66.  
  67.     /*
  68.      * The PATH_INFO for this request.
  69.      */
  70.     private String pathInfo;
  71.  
  72.     /*
  73.      * The SERVLET_PATH for this request.
  74.      */
  75.     private String servletPath;
  76.  
  77.     /*
  78.      * The PATH_TRANSLATED for this request.
  79.      */
  80.     private String pathTranslated;
  81.  
  82.     /*
  83.      * The virtual path to the servlets.
  84.      */
  85.     private final String SERVLETPATH = "/servlet";
  86.  
  87.     /*
  88.      * Possible request header field names.
  89.      */
  90.     private static String[] HEADERS = {
  91.     /* Standard HTTP 1.0 and 1.1 headers */
  92.     "Accept",        "Accept-Charset",    "Accept-Encoding",
  93.     "Accept-Language",    "Accept-Ranges",    "Age",
  94.     "Allow",        "Authorization",    "Cache-Control",
  95.     "Connection",        "Content-Base",        "Content-Encoding",
  96.     "Content-Language",    "Content-Length",    "Content-Location",
  97.     "Content-MD5",        "Content-Range",    "Content-Type",
  98.     "Date",            "ETag",            "Expires",
  99.     "From",            "Host",            "If-Modified-Since",
  100.     "If-Match",        "If-None-Match",    "If-Range",
  101.     "If-Unmodified-Since",    "Last-Modified",    "Location",
  102.     "Max-Forwards",        "Pragma",        "Proxy-Authenticate",
  103.     "Proxy-Authorization",    "Public",        "Range",
  104.     "Referer",        "Retry-After",        "Server",
  105.     "Transfer-Encoding",    "Upgrade",        "User-Agent",
  106.     "Vary",            "Via",            "Warning",
  107.     "WWW-Authenticate",
  108.     /* Popular extension headers */
  109.     "Alternates",        "Content-Version",    "Cookie",
  110.     "Derived-From",        "Keep-Alive",        "Link",
  111.     "MIME-Version",        "URI",            "Title"
  112.     };
  113.  
  114.     ISAPIRequest() {
  115.     conn = null;
  116.     }
  117.  
  118.     ISAPIRequest(ISAPIConnection conn) {
  119.     this.conn = conn;
  120.     in.init(conn);
  121.     }
  122.  
  123.     public void reset() {
  124.     in.resets();
  125.     requestURI = null;
  126.     pathInfo = null;
  127.     servletPath = null;
  128.     pathTranslated = null;
  129.     }
  130.  
  131.     public void init(ISAPIConnection conn) {
  132.     this.conn = conn;
  133.     in.init(conn);
  134.     }
  135.  
  136.     /**
  137.      * Returns the size of the request entity data, or -1 if not known.
  138.      * Same as the CGI variable CONTENT_LENGTH.
  139.      */
  140.     public int getContentLength() {
  141.     String str = conn.getServerVariable("CONTENT_LENGTH");
  142.     int len;
  143.     try {
  144.         len = Integer.parseInt(str);
  145.     } catch (Exception e) {
  146.         return 0;
  147.     }
  148.     return len;
  149.     }
  150.  
  151.     /**
  152.      * Returns the Internet Media Type of the request entity data, or
  153.      * null if not known. Same as the CGI variable CONTENT_TYPE.
  154.      */
  155.     public String getContentType() {
  156.     return conn.getContentType();
  157.     }
  158.  
  159.     /**
  160.      * Returns the protocol and version of the request as a string of the
  161.      * form <protocol>/<major version>.<minor version>.
  162.      * Same as the CGI variable SERVER_PROTOCOL.
  163.      */
  164.     public String getProtocol() {
  165.     return conn.getServerVariable("SERVER_PROTOCOL");
  166.     }
  167.  
  168.     /**
  169.      * Returns the scheme of the request -- always "http" for now.
  170.      */
  171.     public String getScheme() {
  172.     return "http";        // XXX might be "https" too!
  173.     }
  174.  
  175.     /**
  176.      * Returns the host name of the server that received the request.
  177.      * Same as the CGI variable SERVER_NAME.
  178.      */
  179.     public String getServerName() {
  180.     return conn.getServerVariable("SERVER_NAME");
  181.     }
  182.  
  183.     /**
  184.      * Returns the port number on which this request was received.
  185.      * Same as the CGI variable SERVER_PORT.
  186.      */
  187.     public int getServerPort() {
  188.     try {
  189.         return Integer.parseInt(conn.getServerVariable("SERVER_PORT"));
  190.     } catch (NumberFormatException e) {
  191.         return -1;
  192.     }
  193.     }
  194.  
  195.     /**
  196.      * Returns the IP address of the agent that sent the request.
  197.      * Same as the CGI variable REMOTE_ADDR.
  198.      */
  199.     public String getRemoteAddr() {
  200.     return conn.getServerVariable("REMOTE_ADDR");
  201.     }
  202.  
  203.     /**
  204.      * Returns the fully qualified host name of the agent that sent the
  205.      * request. Same as the CGI variable REMOTE_HOST.
  206.      */
  207.     public String getRemoteHost() {
  208.     return conn.getServerVariable("REMOTE_HOST");
  209.     }
  210.  
  211.     /**
  212.      * Applies alias rules to the specified virtual path and returns the
  213.      * corresponding real path. getRealPath("/") is the document root.
  214.      * It returns null if the translation cannot be performed.
  215.      * @param path the path to be translated
  216.      */  
  217.     public String getRealPath(String path) {
  218.     return conn.getRealPath(path);
  219.     }
  220.  
  221.     /**
  222.      * Returns an input stream for reading the request body.
  223.      */
  224.     public ServletInputStream getInputStream() {
  225.     return in;
  226.     }
  227.  
  228.     /**
  229.      * Returns the value of the specified query parameter, or null if none.
  230.      * @param name the query parameter name
  231.      */
  232.     public String getParameter(String name) {
  233.     if (params == null) {
  234.         params = getParameters();
  235.     }
  236.  
  237.     String vals[] = (String []) params.get(name);
  238.  
  239.     if (vals == null)
  240.         return null;
  241.  
  242.     String hackVal = vals[0];
  243.     for (int i = 1; i < vals.length; i++)
  244.         hackVal = hackVal + "," + vals[i];
  245.         
  246.     return hackVal;
  247.     }
  248.  
  249.     /**
  250.      * Returns an array of values for the specified query parameter.
  251.      */
  252.     public String [] getParameterValues(String name) {
  253.     if (params == null)
  254.         params = getParameters();
  255.  
  256.     return (String []) params.get(name);
  257.     }
  258.  
  259.     /**
  260.      * Returns a Dictionary of all the query parameters for this request.
  261.      */
  262.     public Enumeration getParameterNames() {
  263.     if (params == null) {
  264.         params = getParameters();
  265.     }
  266.     return params.keys();
  267.     }
  268.  
  269.     /**
  270.      * Returns an attribute of the request given the specified key name.
  271.      * This allows access to request information not already provided by
  272.      * the other methods in this interface. Key names beginning with
  273.      * 'COM.sun.*' are reserved.
  274.      * @param name the attribute name
  275.      * @return the value of the attribute, or null if not defined
  276.      */
  277.     public Object getAttribute(String name) {
  278.     return null;
  279.     }
  280.  
  281.     /*
  282.      * Return a hashtable of parameters for this request.
  283.      */
  284.     private Hashtable getParameters() {
  285.     Hashtable h = null;
  286.  
  287.     String method = getMethod();
  288.     String queryString = null;
  289.     if (method.equals("GET")) {
  290.         queryString = getQueryString();
  291.     } else if (method.equals("POST")) {
  292.         queryString = getPostedData();
  293.     }
  294.  
  295.     try {
  296.         h = HttpUtils.parseQueryString(queryString);
  297.     } catch (IllegalArgumentException e) {
  298.         h = null;
  299.     }
  300.     return h;
  301.     }
  302.  
  303.     /**
  304.      * Returns the method with which the request was made. The returned
  305.      * value can be "GET", "HEAD", "POST", or an extension method. Same
  306.      * as the CGI variable REQUEST_METHOD.
  307.      */
  308.     public String getMethod() {
  309.     return conn.getMethod();
  310.     }
  311.  
  312.     /**
  313.      * Returns the request URI.
  314.      */
  315.     public String getRequestURI() {
  316.     if (requestURI == null) {
  317.         parsePath();
  318.     }
  319.  
  320.     return requestURI;
  321.     }
  322.  
  323.     /**
  324.      * Returns the part of the request URI that refers to the servlet
  325.      * being invoked. Analogous to the CGI variable SCRIPT_NAME.
  326.      */
  327.     public String getServletPath() {
  328.     if (servletPath == null) {
  329.         parsePath();
  330.     }
  331.  
  332.     return servletPath;
  333.     }
  334.  
  335.     /**
  336.      * Returns optional extra path information following the servlet path,
  337.      * but immediately preceding the query string. Returns null if not
  338.      * specified. Same as the CGI variable PATH_INFO.
  339.      */
  340.     public String getPathInfo() {
  341.     if (pathInfo == null) {
  342.         parsePath();
  343.     }
  344.  
  345.     return pathInfo;
  346.     }
  347.  
  348.     /**
  349.      * Returns extra path information translated to a real path. Returns
  350.      * null if no extra path information specified. Same as the CGI variable
  351.      * PATH_TRANSLATED.
  352.      */
  353.     public String getPathTranslated() {
  354.     if (pathTranslated == null) {
  355.         parsePath();
  356.     }
  357.  
  358.     return pathTranslated;
  359.     }
  360.  
  361.     /**
  362.      * Returns the query string part of the servlet URI, or null if none.
  363.      * Same as the CGI variable QUERY_STRING.
  364.      */
  365.     public String getQueryString() {
  366.     return conn.getQueryString();
  367.     }
  368.  
  369.     /**
  370.      * Returns the name of the user making this request, or null if not
  371.      * known. Same as the CGI variable REMOTE_USER.
  372.      */
  373.     public String getRemoteUser() {
  374.     return conn.getServerVariable("REMOTE_USER");
  375.     }
  376.  
  377.     /**
  378.      * Returns the authentication scheme of the request, or null if none.
  379.      * Same as the CGI variable AUTH_TYPE.
  380.      */
  381.     public String getAuthType() {
  382.     return conn.getServerVariable("AUTH_TYPE");
  383.     }
  384.  
  385.     /**
  386.      * Returns the value of a header field, or null if not found. The
  387.      * case of the header field name is ignored.
  388.      * @param name the header field name
  389.      */
  390.     public String getHeader(String name) {
  391.     return conn.getServerVariable("HTTP_" + name.replace('-', '_'));
  392.     }
  393.  
  394.     /**
  395.      * Returns the value of an integer header field or -1 if not found.
  396.      * The case of the header name is ignored.
  397.      * @param name the header field name
  398.      */
  399.     public int getIntHeader(String name) {
  400.     String value = getHeader(name);
  401.     if (value != null) {
  402.         try {
  403.         return Integer.parseInt(value);
  404.         } catch (NumberFormatException e) {
  405.         return -1;
  406.         }
  407.     }
  408.     return -1;
  409.     }
  410.  
  411.     /**
  412.      * Returns the value of a date header field or -1 if not found. The
  413.      * case of the header field name ignored.
  414.      * @param name the header field name
  415.      */
  416.     public long getDateHeader(String name) {
  417.     String value = getHeader(name);
  418.     if (value != null) {
  419.         try {
  420.         return new Date(value).getTime();
  421.         } catch (IllegalArgumentException e) {
  422.         return -1;
  423.         }
  424.     }
  425.     return -1;
  426.     }
  427.  
  428.     /**
  429.      * Returns a Dictionary of header field names and values for this
  430.      * request. The header field names are guaranteed to be all lower case.
  431.      */
  432.     public Enumeration getHeaderNames() {
  433.     if (headers == null) {
  434.         headers = new Hashtable(HEADERS.length);
  435.         for (int i = 0; i < HEADERS.length; i++) {
  436.         String name = HEADERS[i];
  437.         String value = getHeader(name);
  438.         if (value != null && !value.equals("")) {
  439.             headers.put(name, value);
  440.         }
  441.         }
  442.     }
  443.     return headers.keys();
  444.     }
  445.  
  446.     /*
  447.      * Parses server variables and modifies it for the servlet
  448.      */
  449.     private void parsePath() {
  450.     int index;
  451.     String path = conn.getPathInfo();
  452.     requestURI = SERVLETPATH + path;
  453.     String queryString = conn.getQueryString();
  454.     if (queryString != null && queryString.length() > 0) {
  455.         requestURI = requestURI + "?" + queryString;
  456.     }
  457.     if (path != null) {
  458.         index = path.indexOf('/', 1);
  459.         if (index != -1) {
  460.         servletPath = SERVLETPATH + path.substring(0, index);
  461.         pathInfo = path.substring(index);
  462.         } else {
  463.         servletPath = SERVLETPATH + path.substring(0);
  464.         pathInfo = null;
  465.         }
  466.     }
  467.     if (pathInfo != null) {
  468.         pathTranslated = conn.getRealPath(pathInfo);
  469.     }
  470.     }
  471.  
  472.     /*
  473.      * Fetches the posted data
  474.      */
  475.     private String getPostedData() {
  476.     int len = getContentLength();
  477.     byte[] buf = new byte[len];
  478.     conn.getData(0, buf, 0, len);
  479.     return new String(buf);
  480.     }
  481. }
  482.